| Conditions | 10 |
| Total Lines | 109 |
| Code Lines | 79 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like yii2-floor12-files.js ➔ Yii2FilesUploaderSet often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | console.log('Yii2 files model init.'); |
||
| 67 | function Yii2FilesUploaderSet(id, className, attribute, scenario, name) { |
||
| 68 | |||
| 69 | var mode = 'multi'; |
||
| 70 | var blockName = "#" + id; |
||
| 71 | var block = $(blockName); |
||
| 72 | var uploadButton = block.find('button.btn-upload')[0]; |
||
| 73 | var filesList = block.find('.floor12-files-widget-list')[0]; |
||
| 74 | var ratio = 0; |
||
| 75 | |||
| 76 | var csrf = block.parents('form').find('input[name=' + yii2CsrfParam + ']').val(); |
||
| 77 | |||
| 78 | if (block.data('ratio')) |
||
| 79 | ratio = block.data('ratio'); |
||
| 80 | |||
| 81 | if (block.hasClass('floor12-files-widget-single-block')) { |
||
| 82 | mode = 'single'; |
||
| 83 | toggleSingleUploadButton(block); |
||
| 84 | } |
||
| 85 | |||
| 86 | uploaderSettings[id] = { |
||
| 87 | modelClass: className, |
||
| 88 | attribute: attribute, |
||
| 89 | scenario: scenario, |
||
| 90 | mode: mode, |
||
| 91 | name: name, |
||
| 92 | ratio: ratio, |
||
| 93 | count: block.find('.floor12-files-widget-list .floor12-file-object').length, |
||
| 94 | _fileFormToken: yii2FileFormToken |
||
| 95 | } |
||
| 96 | |||
| 97 | uploaderSettings[id][yii2CsrfParam] = csrf |
||
| 98 | console.log(uploaderSettings[id]); |
||
| 99 | var uploader = new ss.SimpleUpload({ |
||
| 100 | button: uploadButton, |
||
| 101 | url: yii2UploadRoute, |
||
| 102 | name: 'file', |
||
| 103 | dropzone: block, |
||
| 104 | dragClass: 'floor12-files-widget-block-drug-over', |
||
| 105 | multiple: true, |
||
| 106 | multipleSelect: true, |
||
| 107 | data: uploaderSettings[id], |
||
| 108 | onSubmit: function (filename, extension, uploadBtn, size) { |
||
| 109 | uploaderSettings[id].count++; |
||
| 110 | var svg = '\t<svg class="progress-circle" width="60" height="60" viewBox="0 0 60 60">\n' + |
||
| 111 | '\t\t<circle cx="30" cy="30" r="27" fill="none" stroke="#ccc" stroke-width="5" />\n' + |
||
| 112 | '\t\t<circle id="progress-circle" cx="30" cy="30" r="27" fill="none" stroke="#666" stroke-width="5" stroke-dasharray="169.646" stroke-dashoffset="169.646" />\n' + |
||
| 113 | '\t</svg>'; |
||
| 114 | |||
| 115 | var fileId = generateId(filename); |
||
| 116 | var btnGroup = document.createElement('div'); |
||
| 117 | var fileObject = document.createElement('div'); |
||
| 118 | var bar = document.createElement('div'); |
||
| 119 | var percents = document.createElement('div'); |
||
| 120 | btnGroup.setAttribute('id', fileId); |
||
| 121 | btnGroup.className = 'btn-group files-btn-group'; |
||
| 122 | fileObject.className = 'floor12-file-object'; |
||
| 123 | percents.className = 'floor12-file-percents'; |
||
| 124 | this.setProgressBar(bar); |
||
| 125 | |||
| 126 | fileObject.innerHTML = svg; |
||
| 127 | |||
| 128 | observer.observe(bar, { |
||
| 129 | attributes: true |
||
| 130 | }); |
||
| 131 | |||
| 132 | fileObject.appendChild(bar); |
||
| 133 | fileObject.appendChild(percents); |
||
| 134 | btnGroup.appendChild(fileObject); |
||
| 135 | |||
| 136 | if (mode == 'single') { |
||
| 137 | $(filesList).html(''); |
||
| 138 | } |
||
| 139 | $(filesList).append(btnGroup); |
||
| 140 | }, |
||
| 141 | onComplete: function (filename, response) { |
||
| 142 | if (!response) { |
||
| 143 | console.log(filename + 'upload failed'); |
||
| 144 | uploaderSettings[id].count--; |
||
| 145 | return false; |
||
| 146 | } |
||
| 147 | f12notification.info(FileUploadedText, 1); |
||
| 148 | idName = "#" + generateId(filename); |
||
| 149 | $(idName).replaceWith($(response)); |
||
| 150 | if (mode == 'single') |
||
| 151 | toggleSingleUploadButton(block); |
||
| 152 | }, |
||
| 153 | onError: function (filename, errorType, status, statusText, response, uploadBtn, fileSize) { |
||
| 154 | console.log(uploaderSettings[id]); |
||
| 155 | uploaderSettings[id].count--; |
||
| 156 | data = { |
||
| 157 | responseText: response, |
||
| 158 | status: status, |
||
| 159 | statusText: statusText, |
||
| 160 | }; |
||
| 161 | processError(data); |
||
| 162 | idName = "#" + generateId(filename); |
||
| 163 | $(idName).remove(); |
||
| 164 | } |
||
| 165 | |||
| 166 | // progressUrl: 'uploadProgress.php', // enables cross-browser progress support (more info below) |
||
| 167 | // responseType: 'json', |
||
| 168 | // allowedExtensions: ['jpg', 'jpeg', 'png', 'gif'], |
||
| 169 | // maxSize: 1024, // kilobytes |
||
| 170 | // hoverClass: 'ui-state-hover', |
||
| 171 | // focusClass: 'ui-state-focus', |
||
| 172 | // disabledClass: 'ui-state-disabled', |
||
| 173 | }); |
||
| 174 | lastUploader = uploader; |
||
| 175 | } |
||
| 176 | |||
| 411 |